﻿#include "HookThread.h"
#include <globalmouseevent.h>

HookThread::HookThread()
{
    stop=false;
    pause=false;
    i=0;
    max=1;
    connect(GlobalMouseEvent::getInstance(), &GlobalMouseEvent::mouseEvent, this, &HookThread::on_mouseEvent);
}

void HookThread::close()
{
    bool ret = GlobalMouseEvent::removeMouseEvent();
    stop=true;
    quit();
    wait();
}

void HookThread::pauseThread()
{
    qDebug()<<"pauseThread";
    this->pauseLock.lock();
    pause=true;
}

void HookThread::resumeThread()
{
    qDebug()<<"resumeThread";
    this->pauseLock.unlock();
    pause=false;
}

void HookThread::run()
{
    while(i<max)
    {
        if(!stop)
        {
            // 线程锁在业务开始和和结束的地方
            pauseLock.lock();

            // 具体的业务逻辑
            i++;
            qDebug()<<i<<"-"
                   <<QTime::currentTime()
                   <<"-"
                   <<"CurrnetThreadID:"
                   <<QThread::currentThreadId();
//            msleep(1000);
            bool ret = GlobalMouseEvent::installMouseEvent();
            //
        }
        else
        {
            break;
        }
    }
    // 注意！！在线程中一定要加exec才会有event loop，否则SetWindowsHookEx不会有callback返回！！！
    exec();
}

/**
 * @brief       全局鼠标事件
 * @param event
 */
void HookThread::on_mouseEvent(QMouseEvent* event){
    emit sig_mouseEvent(event);
}
